<?php
namespace Admin\Controller;
use Think\Controller;
class ProblemController extends Controller{
    /**
    问题管理
     **/
    public function indexAction(){
        $model = M();

        $sql = "select t1.id,t1.title,t1.views,t1.create_time,t2.username,t3.category_name from question t1 left join user t2 on t1.uid = t2.id left join  category t3 on t1.cid=t3.id";

        $question = $model->query($sql);

        foreach($question as &$value){
            $value['answer_num'] = $this->_countAnswer($value['id']);
        }
//        var_dump($question);
        $this->assign('question',$question);

        $this->display('index');
    }

    public function _countAnswer($id){
        $model = M();

        $sql = "select count(*) cnt from answer where qid='{$id}'";

        $num = $model->query($sql);

        return $num[0]['cnt'];
    }




    /**
    问题删除
     **/
    public function deleteAction(){
        $id = I('get.id');

        $model = M();

        $sql = "delete from question where id='{$id}'";

        $model->execute($sql);

        $this->success('删除成功',U('Admin/Problem/index'),2);

    }

//    /**
//     * 修改问题方法
//     */
//    public function editorAction(){
//        $this->display('editor');
//    }

}